// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import fmod.fmod.FMODManager;
import fmod.fmod.FMOD_STUDIO_EVENT_DESCRIPTION;
import fmod.fmod.FMOD_STUDIO_PLAYBACK_STATE;
import zombie.audio.GameSound;
import zombie.characters.IsoPlayer;
import zombie.core.Core;
import zombie.iso.SpriteDetails.IsoFlagType;
import zombie.network.GameClient;
import zombie.network.GameServer;

public class Alarm {
    protected static long inst;
    protected static FMOD_STUDIO_EVENT_DESCRIPTION event;
    public boolean finished = false;
    private int x;
    private int y;
    private float volume;
    private float occlusion;
    private float endGameTime;

    public Alarm(int _x, int _y) {
        this.x = _x;
        this.y = _y;
        byte _byte = 49;
        float _float = (float)zombie.GameTime.instance.getWorldAgeHours();
        this.endGameTime = _float + (float)_byte / 3600.0F * (1440.0F / zombie.GameTime.instance.getMinutesPerDay());
    }

    public void update() {
        if (!GameClient.bClient) {
            zombie.WorldSoundManager.instance.addSound(this, this.x, this.y, 0, 600, 600);
        }

        if (!GameServer.bServer) {
            this.updateSound();
            if (zombie.GameTime.getInstance().getWorldAgeHours() >= (double)this.endGameTime) {
                if (inst != 0L) {
                    fmod.javafmod.FMOD_Studio_EventInstance_Stop(inst, false);
                    inst = 0L;
                }

                this.finished = true;
            }
        }
    }

    protected void updateSound() {
        if (!GameServer.bServer && !Core.SoundDisabled && !this.finished) {
            if (FMODManager.instance.getNumListeners() != 0) {
                if (inst == 0L) {
                    event = FMODManager.instance.getEventDescription("event:/Meta/HouseAlarm");
                    if (event != null) {
                        fmod.javafmod.FMOD_Studio_LoadEventSampleData(event.address);
                        inst = fmod.javafmod.FMOD_Studio_System_CreateEventInstance(event.address);
                    }
                }

                if (inst > 0L) {
                    float float0 = zombie.SoundManager.instance.getSoundVolume();
                    GameSound gameSound = zombie.GameSounds.getSound("HouseAlarm");
                    if (gameSound != null) {
                        float0 *= gameSound.getUserVolume();
                    }

                    if (float0 != this.volume) {
                        fmod.javafmod.FMOD_Studio_EventInstance_SetVolume(inst, float0);
                        this.volume = float0;
                    }

                    fmod.javafmod.FMOD_Studio_EventInstance3D(inst, (float)this.x, (float)this.y, 0.0F);
                    if (fmod.javafmod.FMOD_Studio_GetPlaybackState(inst) != FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_PLAYING.index
                        && fmod.javafmod.FMOD_Studio_GetPlaybackState(inst) != FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_STARTING.index) {
                        if (fmod.javafmod.FMOD_Studio_GetPlaybackState(inst) == FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_STOPPING.index) {
                            this.finished = true;
                            return;
                        }

                        fmod.javafmod.FMOD_Studio_StartEvent(inst);
                        System.out.println(fmod.javafmod.FMOD_Studio_GetPlaybackState(inst));
                    }

                    float float1 = 0.0F;
                    if (IsoPlayer.numPlayers == 1) {
                        IsoGridSquare isoGridSquare0 = IsoPlayer.getInstance().getCurrentSquare();
                        if (isoGridSquare0 != null && !isoGridSquare0.Is(IsoFlagType.exterior)) {
                            float1 = 0.2F;
                            IsoGridSquare isoGridSquare1 = IsoWorld.instance.getCell().getGridSquare(this.x, this.y, 0);
                            if (isoGridSquare1 != null && isoGridSquare1.getBuilding() == isoGridSquare0.getBuilding()) {
                                float1 = 0.0F;
                            }
                        }
                    }

                    if (this.occlusion != float1) {
                        this.occlusion = float1;
                        fmod.javafmod.FMOD_Studio_EventInstance_SetParameterByName(inst, "Occlusion", this.occlusion);
                    }
                }
            }
        }
    }
}
